package org.xin.security.controller;

import cn.hutool.core.lang.Assert;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import org.xin.common.result.Result;
import org.xin.common.util.XinUtil;
import org.xin.security.component.UserLoginComponent;
import org.xin.security.component.UserRegisterComponent;
import org.xin.security.pojo.entity.SysUserSecurity;
import org.xin.security.pojo.form.UserLoginForm;
import org.xin.security.pojo.form.UserRegisterForm;
import org.xin.security.service.ISysUserSecurityService;

import org.xin.framework.base.controller.IBaseController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
* <p>
    * 系统模块-用户安全表 前端控制器
    * </p>
*
* @author: xin
* @date: Create in 2021-09-29
*/
@Api(tags = "系统模块-用户安全")
@CrossOrigin
@RestController
@RequestMapping("/sysUserSecurity")
public class SysUserSecurityController extends IBaseController<SysUserSecurity, ISysUserSecurityService> {

    @Autowired
    private UserLoginComponent loginComponent;
    @Autowired
    private UserRegisterComponent registerComponent;


    @ApiOperation("登录")
    @PostMapping("/login")
    public Result login(HttpServletRequest request, HttpServletResponse response,
                        @Validated @RequestBody UserLoginForm form){
        return Result.success(loginComponent.login(form));
    }

    @ApiOperation(value = "注册", produces = "application/json;charset=utf-8")
    @PostMapping("/register")
    public Result register(HttpServletRequest request, HttpServletResponse response,
                           @Validated @RequestBody UserRegisterForm form){
        Assert.isTrue(XinUtil.equal(form.getPassword(), form.getPassword2()), "两次密码不一致");
        return Result.check(registerComponent.register(form));
    }
}

